package com.xiaoyu.izf.service;

import com.xiaoyu.izf.model.CommissionLog;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.VipInfo;
import com.xiaoyu.izf.model.VipOrder;
import com.xiaoyu.izf.repository.CommissionLogDao;
import com.xiaoyu.izf.repository.UserInfoDao;
import com.xiaoyu.izf.repository.VipInfoDao;
import com.xiaoyu.izf.repository.VipOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

@Service
public class VipOrderService {
    @Autowired
    private VipOrderDao vipOrderDao;
    @Autowired
    private VipInfoDao vipInfoDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private CommissionLogDao commissionLogDao;

    @Transactional
    public VipOrder orderAdd(String orderId, int vipId, UserInfo user){
        VipInfo vip = vipInfoDao.findOneById(vipId);
        VipOrder order = new VipOrder();
        order.setId(orderId);
        order.setUid(user.getId());
        order.setVipType(vipId);
        order.setOrderStatus(0);
        order.setOutTradeNo("");
        order.setInsertTime(new Timestamp(System.currentTimeMillis()));
        order.setPayTime(new Timestamp(System.currentTimeMillis()));
        order.setIsTest(user.getIsTest());
        order.setType(0);
        int payAmount = 0;//购买或者升级vip需要支付的金额
        //非vip用户，直接购买vip
        if (user.getVipType()==0)
            payAmount = vip.getVipPrice();
        //已是vip用户，升级vip
        else{
            payAmount = vip.getVipPrice()-vipInfoDao.findOneById(user.getVipType()).getVipPrice();
        }

        order.setTotAmount(payAmount);
        //存在上级推广人
        if(user.getPushUid()>0&&user.getIsTest()==0){
            UserInfo pushUser = userInfoDao.findOneById(user.getPushUid());
            if(pushUser.getVipType()>0) {
                //上级推广人vip
                VipInfo vip1 = vipInfoDao.findOneById(pushUser.getVipType());
                int commissioin = payAmount * vip1.getVipSplite() / 100;
                order.setPushUid(pushUser.getId());
                order.setPushSplit(vip1.getVipSplite());
                order.setPushAmount(commissioin);
                commissionLogAdd(orderId, pushUser, vip1, 1, payAmount, commissioin, user.getId());
            }
        }else{
            order.setPushUid(0);
            order.setPushSplit(0);
            order.setPushAmount(0);
        }
        //存在上上级推广人
        if(user.getGradePushUid()>0&&user.getIsTest()==0){
            UserInfo pushUser = userInfoDao.findOneById(user.getGradePushUid());
            if(pushUser.getVipType()>0) {
                //上上级推广人vip
                VipInfo vip1 = vipInfoDao.findOneById(pushUser.getVipType());
                int commissioin = payAmount * vip1.getVipGradeSplite() / 100;
                order.setGradePushUid(pushUser.getId());
                order.setGradeSplit(vip1.getVipGradeSplite());
                order.setGradeAmount(commissioin);
                commissionLogAdd(orderId, pushUser, vip1, 2, payAmount, commissioin, user.getId());
            }
        }else {
            order.setGradePushUid(0);
            order.setGradeSplit(0);
            order.setGradeAmount(0);
        }
        return vipOrderDao.save(order);
    }

    public void commissionLogAdd(String orderId, UserInfo pushUser, VipInfo vip, int gradeType, int totAmount, int commission, int sourceUid){
        CommissionLog log = new CommissionLog();
        log.setUid(pushUser.getId());
        log.setUserVipType(vip.getId());
        log.setGradeType(gradeType);
        log.setTotalAmount(totAmount);
        log.setOrderId(orderId);
        log.setCommissioin(commission);
        log.setLogType(1);
        log.setLogTime(new Timestamp(System.currentTimeMillis()));
        log.setPayStatus(0);
        log.setSourceUid(sourceUid);
        log.setIsOnline(1);
        commissionLogDao.save(log);
    }

    public VipOrder vipOrder(String orderId){
        return vipOrderDao.findOneById(orderId);
    }
}
